/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.androidgap.plugin;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;

import com.worklight.androidgap.NoSuchResourceException;
import com.worklight.androidgap.WLAndroidUtils;
import com.worklight.androidgap.WLDroidGap;

public class WLMenuItem {
	
	private int 	id;
	private String 	javascriptId; 
	private String 	callback;
	private String 	title;
	private String 	imagePath;
	private boolean isEnabled;
	private boolean hasChanged;
	private Drawable drawable; 
	
	public WLMenuItem(int id, String javascriptId, String callback, String title, String imagePath, boolean isEnabled){
		this.id 		= id;
		this.javascriptId = javascriptId;
		this.callback 	= callback;
		this.title 		= title;		
		this.isEnabled 	= isEnabled;
		setImagePath(imagePath);
		this.hasChanged = true;
	}

	public int getId(){
		return id;
	}
	
	public String getJavaScriptId(){
		return javascriptId;
	}
	
	public String getCallback() {
		return callback;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		setChanged();
	}

	public String getImagePath() {
		return imagePath;
	}
	
	public void setImagePath(String imagePath) {		
		if (!imagePath.equals(this.imagePath)){
			this.imagePath = imagePath;
			this.drawable = null;
			setChanged();
		}
	}
	
	public Drawable getImage(WLDroidGap wlDroidGap, String skinName) {
		Drawable drawable = null;
		if (imagePath != null){
			Resources res = wlDroidGap.getResources();
			try {
			int drawableId = WLAndroidUtils.getResourceId(wlDroidGap, "drawable", imagePath);
			drawable = res.getDrawable(drawableId);			
			} catch (NoSuchResourceException e) {
				WLAndroidUtils.error(e.getMessage());
			}						
		}
		return drawable;
	}

	public boolean isEnabled() {
		return isEnabled;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
		setChanged();
	}

	public void unsetChanged(){
		hasChanged = false;
	}
	
	public boolean hasChanged(){		
		return hasChanged;
	}

	private void setChanged(){
		hasChanged = true;
	}
	
	enum Density {
		ldpi, mdpi, hdpi, xhdpi
	}
}
